<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>自定义表单权限</title>
    <#include "/pubHeadStyle.html">
    <style>
        .layui-layout-body{overflow: auto}
    </style>
</head>
<body>
<form class="layui-form formDef" lay-filter="formData" id="permission" onsubmit="return false"
      style="height: 620px;overflow-y: auto">


    <div style="overflow:auto;width: 100%; height: 482px;" id="mainTable">
        <div style="padding-left: 33%">
            <input type="radio" name="mainRadio" lay-filter="mainRadio" value="1" title="隐藏">
            <input type="radio" name="mainRadio" lay-filter="mainRadio" value="2" title="只读">
            <input type="radio" name="mainRadio" lay-filter="mainRadio" value="3" title="编辑">
            <input type="radio" name="mainRadio" lay-filter="mainRadio" value="4" title="必填">
        </div>
        <table class="layui-hide" id="table" lay-filter="table"></table>
    </div>
    <div class="layui-btn-container" style="left: 38%;text-align: center">
        <button type="button" class="layui-btn" lay-submit lay-filter="save">保存</button>
        <button class="layui-btn layui-btn-danger" id="return">取消</button>
        <input type="hidden" name="return">
        <button class="layui-btn layui-btn" id="resetPermission">重置权限</button>
    </div>
</form>
<script type="text/html" id="selectReadTemplate">
    <select onchange="inputEmpty(this)" lay-ignore name="{{d.tableId+d.title}}">
        <option value="everyone">所有人</option>
        <option value="user">用户</option>
        <option value="role">角色</option>
        <option value="org">组织</option>
        <option value="orgMgr">组织负责人</option>
        <option value="pos">岗位</option>
        <option value="none">无</option>
    </select>
    {{#  if(d.read.type != 'everyone' && d.read.type != 'none'){ }}
    <div name="{{d.tableId+d.title}}">
        <input type="hidden" value="{{d.read.id}}" id="{{'ids'+d.tableId+d.title+'0'}}"/>
        <input type="text" value="{{d.read.fullname}}" id="{{'names'+d.tableId+d.title+'0'}}" style="width:100%"
               readonly/><br>
        <a class="layui-btn layui-btn-xs" onclick="selectButton(this)">选择</a>
    </div>
    {{#  } else { }}
    {{#  } }}
</script>
<script type="text/html" id="selectWriteTemplate">
    <select onchange="inputEmpty(this)" lay-ignore name="{{d.tableId+d.title}}">
        <option value="everyone">所有人</option>
        <option value="user">用户</option>
        <option value="role">角色</option>
        <option value="org">组织</option>
        <option value="orgMgr">组织负责人</option>
        <option value="pos">岗位</option>
        <option value="none">无</option>
    </select>
    {{#  if(d.write.type != 'everyone' && d.write.type != 'none'){ }}
    <div name="{{d.tableId+d.title}}">
        <input type="hidden" value="{{d.write.id}}" id="{{'ids'+d.tableId+d.title+'1'}}"/>
        <input type="text" value="{{d.write.fullname}}" id="{{'names'+d.tableId+d.title+'1'}}" style="width:100%"
               readonly/><br>
        <a class="layui-btn layui-btn-xs" onclick="selectButton(this)">选择</a>
    </div>
    {{#  } else { }}
    {{#  } }}
</script>
<script type="text/html" id="selectRequiredTemplate">
    <select onchange="inputEmpty(this)" lay-ignore name="{{d.tableId+d.title}}">
        <option value="everyone">所有人</option>
        <option value="user">用户</option>
        <option value="role">角色</option>
        <option value="org">组织</option>
        <option value="orgMgr">组织负责人</option>
        <option value="pos">岗位</option>
        <option value="none">无</option>
    </select>
    {{#  if(d.required.type != 'everyone' && d.required.type != 'none'){ }}
    <div name="{{d.tableId+d.title}}">
        <input type="hidden" value="{{d.required.id}}" id="{{'ids'+d.tableId+d.title+'2'}}"/>
        <input type="text" value="{{d.required.fullname}}" id="{{'names'+d.tableId+d.title+'2'}}" style="width:100%"
               readonly/><br>
        <a class="layui-btn layui-btn-xs" onclick="selectButton(this)">选择</a>
    </div>
    {{#  } else { }}
    {{#  } }}
</script>
<script type="text/html" id="radioTemplate">
    <input type="checkbox" lay-filter="tableInBox" lay-skin="primary" name="{{d.tableId+d.title}}">
</script>
<script src="${ctx}/extends/jquery.min.js" type="text/javascript"></script>
<script>
    //每个select的'选择'点击事件
    function selectButton(ele) {
        var element = $(ele).parent().prev();
        var attrId = element.attr('id');//选择的selectId
        var selectValue = element.find("option:selected").val();//选择的select的值
        //人员选择器
        if (selectValue == 'user') {
            var userSelectorWin;
            var body;
            var userNameArray = [], userIdArray = [];
            layer.open({
                type: 2,
                title: "人员选择器",
                shade: 0.3,
                resize: false,
                area: ['1000px', '540px'],
                btn:["保存","取消","撤销"],
                content: serverPath.formPath + setter.sysLinkPath + "dialog/view/sysUserDialog",
                success: function (layero, index) {
                    body = layer.getChildFrame('body',index);
                    userSelectorWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                    userSelectorWin.layui.userList = [];
                    var names = $("#names" + attrId).val();
                    var ids = $("#ids" + attrId).val();
                    if (ids != "" && names != "") {
                        userNameArray = names.split(",");
                        userIdArray = ids.split(",");
                        for (var i = 0; i < userIdArray.length; i++) {
                            userSelectorWin.layui.userList.push({name: userNameArray[i], userId: userIdArray[i]});
                        }
                    }
                },
                yes: function (index,layero) {
                    userNameArray = [], userIdArray = [];
                    var theUserList = body.find("input[name=userList]").val();
                    if(theUserList){
                        layui.each(JSON.parse(theUserList), function (index, item) {
                            userNameArray.push(item.name);
                            userIdArray.push(item.userId);
                        });
                    }
                    $("#names" + attrId).val(userNameArray.join(","));
                    $("#ids" + attrId).val(userIdArray.join(","));
                    layer.close(index);
                },
                btn3:function(index, layero){
                    userSelectorWin.cancelData(index);
                    return false //开启该代码可禁止点击该按钮关闭
                }
            });
        }
        //组织 或 组织负责人 选择器
        if (selectValue == 'org' || selectValue == 'orgMgr') {
            var orgSelectorWin;
            var body;
            var orgNameArray = [], orgIdArray = [];
            layer.open({
                type: 2,
                title: "组织选择器",
                shade: 0.3,
                resize: false,
                area: ['1000px', '540px'],
                btn:["保存","取消","撤销"],
                content: serverPath.formPath + setter.sysLinkPath + "dialog/view/sysOrgDialog",
                success: function (layero, index) {
                    body = layer.getChildFrame('body',index);
                    orgSelectorWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                    orgSelectorWin.layui.orgList = [];
                    var names = $("#names" + attrId).val();
                    var ids = $("#ids" + attrId).val();
                    if (ids != "" && names != "") {
                        orgNameArray = names.split(",");
                        orgIdArray = ids.split(",");
                        for (var i = 0; i < orgIdArray.length; i++) {
                            orgSelectorWin.layui.orgList.push({name: orgNameArray[i], orgId: orgIdArray[i]});
                        }
                    }
                },
                yes: function (index,layero) {
                    orgNameArray = [], orgIdArray = [];
                    var theOrgList = body.find("input[name=orgList]").val();
                    if(theOrgList){
                        layui.each(JSON.parse(theOrgList), function (index, item) {
                            orgNameArray.push(item.name);
                            orgIdArray.push(item.orgId);
                        });
                    }
                    $("#names" + attrId).val(orgNameArray.join(","));
                    $("#ids" + attrId).val(orgIdArray.join(","));
                    layer.close(index);
                },
                btn3:function(index, layero){
                    orgSelectorWin.cancelData(index);
                    return false //开启该代码可禁止点击该按钮关闭
                }
            });
        }
        //角色选择器
        if (selectValue == 'role') {
            var roleSelectorWin;
            var body;
            var roleNameArray = [], roleIdArray = [];
            layer.open({
                type: 2,
                title: "角色选择器",
                shade: 0.3,
                resize: false,
                area: ['1000px', '540px'],
                btn:["保存","取消","撤销"],
                content: serverPath.formPath + setter.sysLinkPath + "dialog/view/sysRoleDialog",
                success: function (layero, index) {
                    body = layer.getChildFrame('body',index);
                    roleSelectorWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                    roleSelectorWin.layui.roleList = [];
                    var names = $("#names" + attrId).val();
                    var ids = $("#ids" + attrId).val();
                    if (ids != "" && names != "") {
                        roleNameArray = names.split(",");
                        roleIdArray = ids.split(",");
                        for (var i = 0; i < roleIdArray.length; i++) {
                            roleSelectorWin.layui.roleList.push({name: roleNameArray[i], roleId: roleIdArray[i]});
                        }
                    }
                },
                yes: function (index,layero) {
                    roleNameArray = [], roleIdArray = [];
                    var theRoleList = body.find("input[name=roleList]").val();
                    if(theRoleList){
                        layui.each(JSON.parse(theRoleList), function (index, item) {
                            roleNameArray.push(item.name);
                            roleIdArray.push(item.roleId);
                        });
                    }
                    $("#names" + attrId).val(roleNameArray.join(","));
                    $("#ids" + attrId).val(roleIdArray.join(","));
                    layer.close(index);
                },
                btn3:function(index, layero){
                    roleSelectorWin.cancelData(index);
                    return false //开启该代码可禁止点击该按钮关闭
                }
            });
        }
    }

    //点击select将其下的input置空
    function inputEmpty(ele) {
        var element = $(ele);
        var attrId = element.attr('id');//选择的selectId
        var ids = 'ids' + attrId;
        var names = 'names' + attrId;
        var selectValue = element.find("option:selected").val();//选择的select的值
        element.next('div').remove();//先删除下一个兄弟div
        if (selectValue == 'user' || selectValue == 'role' || selectValue == 'org' || selectValue == 'orgMgr' || selectValue == 'pos') {
            //上一步已经删除了div，这一步在元素后接一个空的div
            element.after('<div>\n' +
                '<input type="hidden" value="" id="' + ids + '"/>\n' +
                '<input type="text" value="" id="' + names + '" style="width:100%" readonly/><br>\n' +
                '<a class="layui-btn layui-btn-xs" onclick="selectButton(this)" >选择</a>\n' +
                '</div>');
        }
        //判断所点击select是哪个
        //主表字段
        if (res.field.length > 0) {
            for (var i = 0; i < res.field.length; i++) {
                //read
                if (res.field[i].tableId + res.field[i].title + 0 == attrId) {
                    res.field[i].read.type = selectValue;
                    res.field[i].read.id = '';
                    res.field[i].read.fullname = '';
                    return false;
                }
                //write
                if (res.field[i].tableId + res.field[i].title + 1 == attrId) {
                    res.field[i].write.type = selectValue;
                    res.field[i].write.id = '';
                    res.field[i].write.fullname = '';
                    return false;
                }
                //required
                if (res.field[i].tableId + res.field[i].title + 2 == attrId) {
                    res.field[i].required.type = selectValue;
                    res.field[i].required.id = '';
                    res.field[i].required.fullname = '';
                    return false;
                }
            }
        }
        //子表字段
        if (res.table.length > 0) {
            for (var j = 0; j < res.table.length; j++) {
                if (res.table[j].subField.length > 0) {
                    for (var b = 0; b < res.table[j].subField.length; b++) {
                        //read
                        if (res.table[j].subField[b].tableId + res.table[j].subField[b].title + 0 == attrId) {
                            res.table[j].subField[b].read.type = selectValue;
                            res.table[j].subField[b].read.id = '';
                            res.table[j].subField[b].read.fullname = '';
                            return false;
                        }
                        //write
                        if (res.table[j].subField[b].tableId + res.table[j].subField[b].title + 1 == attrId) {
                            res.table[j].subField[b].write.type = selectValue;
                            res.table[j].subField[b].write.id = '';
                            res.table[j].subField[b].write.fullname = '';
                            return false;
                        }
                        //required
                        if (res.table[j].subField[b].tableId + res.table[j].subField[b].title + 2 == attrId) {
                            res.table[j].subField[b].required.type = selectValue;
                            res.table[j].subField[b].required.id = '';
                            res.table[j].subField[b].required.fullname = '';
                            return false;
                        }
                    }
                }
            }
        }

    }

    var res;   //返回的所有的数据
    var table; //layui.table
    var layer; //layui.layer
    var setter;//layui.setter
    layui.config({
        base: serverPath.formPath + staticPath.formPath //静态资源所在路径
    }).extend({
        index: 'index' //主入口模块
    }).use(['index', 'table', 'form', 'jquery', 'setter', 'layer','commons'], function () {
        table = layui.table;
        var form = layui.form;
        var $ = layui.$;
        setter = layui.setter;
        layer = layui.layer;
        var commons = layui.commons;
        var index = parent.layer.getFrameIndex(window.name);
        $("input[name=return]").val("true");
        $('#return').click(function () {
            $("input[name=return]").val("false");
            parent.layer.close(index);
        });
        //重置权限
        $('#resetPermission').click(function () {
            layer.confirm('确定要重置权限设置吗?', function (index1) {
                $.ajax({
                    url: serverPath.formPath + setter.formLinkPath + "formDef/resetPermission",
                    data: {"formKey": formKey,"actDefId": actDefId, "nodeId":nodeId, "parentActDefId": parentActDefId},
                    dataType: 'json',
                    type: 'post',
                    success: function (data) {
                        if (commons._filterRecord(data)) {
                            layer.alert(data.msg,{skin: 'btn-class'}, function () {
                                commons._bulletWindow(index);
                            });
                        } else {
                            window.parent.layer.alert(data.msg);
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        layer.alert("重置失败！");
                    }
                });
                return false;
            })
        });


        var formKey = '${formKey}';
        var nodeId = "${nodeId}";
        var actDefId = "${procDefId}";
        var isNodeRights = ${isNodeRights?string('true','false')};
        var parentActDefId = "${parentProcDefId}";

        //保存按钮
        form.on('submit(save)', function () {
            layui.each($("select"), function (index, item) {
                var elem = $(item).val();
                if (elem != 'everyone' && elem != 'none') {
                    var attrId = $(item).attr('id');//选择的selectId
                    var ids = '#ids' + attrId;//input ids
                    var names = '#names' + attrId;//input names
                    var idsValue = $(ids).val();
                    var namesValue = $(names).val();

                    //主表字段
                    if (res.field.length > 0) {
                        for (var i = 0; i < res.field.length; i++) {
                            //read
                            if (res.field[i].tableId + res.field[i].title + 0 == attrId) {
                                res.field[i].read.id = idsValue;
                                res.field[i].read.fullname = namesValue;
                                return false;
                            }
                            //write
                            if (res.field[i].tableId + res.field[i].title + 1 == attrId) {
                                res.field[i].write.id = idsValue;
                                res.field[i].write.fullname = namesValue;
                                return false;
                            }
                            //required
                            if (res.field[i].tableId + res.field[i].title + 2 == attrId) {
                                res.field[i].required.id = idsValue;
                                res.field[i].required.fullname = namesValue;
                                return false;
                            }
                        }
                    }
                    //子表字段
                    if (res.table.length > 0) {
                        for (var j = 0; j < res.table.length; j++) {
                            if (res.table[j].subField.length > 0) {
                                for (var b = 0; b < res.table[j].subField.length; b++) {
                                    //read
                                    if (res.table[j].subField[b].tableId + res.table[j].subField[b].title + 0 == attrId) {
                                        res.table[j].subField[b].read.id = idsValue;
                                        res.table[j].subField[b].read.fullname = namesValue;
                                        return false;
                                    }
                                    //write
                                    if (res.table[j].subField[b].tableId + res.table[j].subField[b].title + 1 == attrId) {
                                        res.table[j].subField[b].write.id = idsValue;
                                        res.table[j].subField[b].write.fullname = namesValue;
                                        return false;
                                    }
                                    //required
                                    if (res.table[j].subField[b].tableId + res.table[j].subField[b].title + 2 == attrId) {
                                        res.table[j].subField[b].required.id = idsValue;
                                        res.table[j].subField[b].required.fullname = namesValue;
                                        return false;
                                    }
                                }
                            }
                        }
                    }


                }
            });

            var subTable = new Array();

            if (res.table.length > 0) {
                for (var j = 0; j < res.table.length; j++) {
                    if (res.table[j].subField.length > 0) {
                        for (var b = 0; b < res.table[j].subField.length; b++) {
                            subTable.push(res.table[j].subField[b])
                        }
                    }
                }
            }
            res.subTableShow = res.tableShow;
            res.subTable = subTable;
            /*delete (res.table);
            delete (res.tableShow);*/

            $.ajax({
                url: serverPath.formPath + setter.formLinkPath + "formDef/savePermission",
                data: {"formKey": formKey, "permission": JSON.stringify(res),"actDefId": actDefId, "nodeId":nodeId, "parentActDefId": parentActDefId},
                dataType: 'json',
                type: 'post',
                success: function (data) {
                    if (commons._filterRecord(data)) {
                        layer.alert(data.msg,{skin: 'btn-class'}, function () {
                            commons._bulletWindow(index);
                        });
                    } else {
                        window.parent.layer.alert(data.msg);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    layer.alert("保存失败！");
                }
            })
        });

        //监听主表中的checkbox
        form.on('checkbox(tableInBox)', function(data){
            //获取table某一行的select 根据name属性 tableId+字段 唯一
            var attrName = $(data.elem).attr('name');
            //修改主表select check属性  并修改主表字段
            if(data.elem.checked){
                layui.each($("select[name='"+attrName+"']"), function (index, item) {
                    var elem = $(item);
                    if(index==0){
                        elem.val('everyone');
                        //将其下的input remove掉
                        elem.next('div').remove();
                    }
                    if(index==1){
                        elem.val('none');
                        //将其下的input remove掉
                        elem.next('div').remove();
                    }
                    if(index==2){
                        elem.val('none');
                        //将其下的input remove掉
                        elem.next('div').remove();
                    }
                });
                form.render('select');

                if(res.field.length>0){
                    for(var i = 0; i < res.field.length; i++){
                        if(res.field[i].tableId+res.field[i].title==attrName){
                            res.field[i].read.type='everyone';
                            res.field[i].write.type='none';
                            res.field[i].required.type='none';
                            res.field[i].rpost=true;
                            return false;
                        }

                    }
                }
            }else{
                if(res.field.length>0){
                    for(var k = 0; k < res.field.length; k++){
                        if(res.field[k].tableId+res.field[k].title==attrName){
                            res.field[k].rpost=false;
                            return false;
                        }
                    }
                }
            }
        });
        //监听子表表头checkbox
        form.on('checkbox(subCheckBox)', function(data){
            for(var j = 0; j < res.tableShow.length; j++){
                if($(data.elem).attr('name')==res.tableShow[j].tableId+0){
                    if(data.elem.checked){
                        $(data.elem).next().next().prop("checked",true);
                        $(data.elem).next().next().next().next().prop("checked",true);
                        res.tableShow[j].show.y = "true";
                        res.tableShow[j].show.b = "true";
                        res.tableShow[j].show.addRow = "true";
                    }else{
                        res.tableShow[j].show.y = "false";
                    }
                }
                if($(data.elem).attr('name')==res.tableShow[j].tableId+1){
                    if(data.elem.checked){
                        $(data.elem).next().next().prop("checked",true);
                        res.tableShow[j].show.y = "true";
                        res.tableShow[j].show.addRow = "true";
                    }else{
                        res.tableShow[j].show.y = "false";
                    }
                }
                if($(data.elem).attr('name')==res.tableShow[j].tableId+2){
                    if(data.elem.checked){
                        res.tableShow[j].show.addRow = "true";
                    }else{
                        res.tableShow[j].show.addRow = "false";
                    }
                }
                form.render('checkbox');
            }
        });
        //监听主表radio
        form.on('radio(mainRadio)', function(data){
            if(res.field.length>0){
                //隐藏
                if(data.value==1){
                    for(var i = 0; i < res.field.length; i++){
                        layui.each($("select[name='"+res.field[i].tableId+res.field[i].title+"']"), function (index, item) {
                            var elem = $(item);
                            elem.val('none');
                            //将其下的input remove掉
                            elem.next('div').remove();
                            //只读
                            if(index==0){
                                res.field[i].read.type='none';
                            }
                            //编辑
                            if(index==1){
                                res.field[i].write.type='none';
                            }
                            //必填
                            if(index==2){
                                res.field[i].required.type='none';
                            }
                        });
                    }
                }
                //只读
                if(data.value==2){
                    for(var i = 0; i < res.field.length; i++){
                        layui.each($("select[name='"+res.field[i].tableId+res.field[i].title+"']"), function (index, item) {
                            var elem = $(item);
                            //将其下的input remove掉
                            elem.next('div').remove();
                            //只读
                            if(index==0){
                                elem.val('everyone');
                                res.field[i].read.type='everyone';
                            }
                            //编辑
                            if(index==1){
                                elem.val('none');
                                res.field[i].write.type='none';
                            }
                            //必填
                            if(index==2){
                                elem.val('none');
                                res.field[i].required.type='none';
                            }
                        });
                    }
                }
                //编辑
                if(data.value==3){
                    for(var i = 0; i < res.field.length; i++){
                        layui.each($("select[name='"+res.field[i].tableId+res.field[i].title+"']"), function (index, item) {
                            var elem = $(item);
                            //将其下的input remove掉
                            elem.next('div').remove();
                            //只读
                            if(index==0){
                                elem.val('everyone');
                                res.field[i].read.type='everyone';
                            }
                            //编辑
                            if(index==1){
                                elem.val('everyone');
                                res.field[i].write.type='everyone';
                            }
                            //必填
                            if(index==2){
                                elem.val('none');
                                res.field[i].required.type='none';
                            }
                        });
                    }
                }
                //必填
                if(data.value==4){
                    for(var i = 0; i < res.field.length; i++){
                        layui.each($("select[name='"+res.field[i].tableId+res.field[i].title+"']"), function (index, item) {
                            var elem = $(item);
                            elem.val('everyone');
                            //将其下的input remove掉
                            elem.next('div').remove();
                            //只读
                            if(index==0){
                                res.field[i].read.type='everyone';
                            }
                            //编辑
                            if(index==1){
                                res.field[i].write.type='everyone';
                            }
                            //必填
                            if(index==2){
                                res.field[i].required.type='everyone';
                            }
                        });
                    }
                }
            }
            form.render('select');
        });

        //监听子表radio
        form.on('radio(subRadio)', function(data){
            if(res.table.length>0){
                for(var j = 0; j < res.table.length; j++){
                    if(res.table[j].subField.length>0){
                        //隐藏
                        if(data.value==1){
                            for(var b = 0; b < res.table[j].subField.length; b++){
                                layui.each($("select[name='"+res.table[j].subField[b].tableId+res.table[j].subField[b].title+"']"), function (index, item) {
                                    var elem = $(item);
                                    elem.val('none');
                                    //将其下的input remove掉
                                    elem.next('div').remove();
                                    //只读
                                    if(index==0){
                                        res.table[j].subField[b].read.type='none';
                                    }
                                    //编辑
                                    if(index==1){
                                        res.table[j].subField[b].write.type='none';
                                    }
                                    //必填
                                    if(index==2){
                                        res.table[j].subField[b].required.type='none';
                                    }
                                });
                            }
                        }
                        //只读
                        if(data.value==2){
                            for(var b = 0; b < res.table[j].subField.length; b++){
                                layui.each($("select[name='"+res.table[j].subField[b].tableId+res.table[j].subField[b].title+"']"), function (index, item) {
                                    var elem = $(item);
                                    //将其下的input remove掉
                                    elem.next('div').remove();
                                    //只读
                                    if(index==0){
                                        elem.val('everyone');
                                        res.table[j].subField[b].read.type='everyone';
                                    }
                                    //编辑
                                    if(index==1){
                                        elem.val('none');
                                        res.table[j].subField[b].write.type='none';
                                    }
                                    //必填
                                    if(index==2){
                                        elem.val('none');
                                        res.table[j].subField[b].required.type='none';
                                    }
                                });
                            }
                        }
                        //编辑
                        if(data.value==3){
                            for(var b = 0; b < res.table[j].subField.length; b++){
                                layui.each($("select[name='"+res.table[j].subField[b].tableId+res.table[j].subField[b].title+"']"), function (index, item) {
                                    var elem = $(item);
                                    //将其下的input remove掉
                                    elem.next('div').remove();
                                    //只读
                                    if(index==0){
                                        elem.val('everyone');
                                        res.table[j].subField[b].read.type='everyone';
                                    }
                                    //编辑
                                    if(index==1){
                                        elem.val('everyone');
                                        res.table[j].subField[b].write.type='everyone';
                                    }
                                    //必填
                                    if(index==2){
                                        elem.val('none');
                                        res.table[j].subField[b].required.type='none';
                                    }
                                });
                            }
                        }
                        //必填
                        if(data.value==4){
                            for(var b = 0; b < res.table[j].subField.length; b++){
                                layui.each($("select[name='"+res.table[j].subField[b].tableId+res.table[j].subField[b].title+"']"), function (index, item) {
                                    var elem = $(item);
                                    elem.val('everyone');
                                    //将其下的input remove掉
                                    elem.next('div').remove();
                                    //只读
                                    if(index==0){
                                        res.table[j].subField[b].read.type='everyone';
                                    }
                                    //编辑
                                    if(index==1){
                                        res.table[j].subField[b].write.type='everyone';
                                    }
                                    //必填
                                    if(index==2){
                                        res.table[j].subField[b].required.type='everyone';
                                    }
                                });
                            }
                        }
                    }
                }
                form.render('select');
            }
        });

        if (isNodeRights) {
            // 处理节点权限
            $.ajax({
                url: serverPath.formPath + setter.formLinkPath +"formDef/getPermissionByFormNode",
                type: 'post',
                async: false, //更改为同步
                dataType: 'json',
                data: {'formKey':formKey,'actDefId':actDefId,'nodeId':nodeId,'parentActDefId':parentActDefId},
                success: function (result) {
                    dealResult(result);
                }
            });

        } else if (actDefId) {
            // 流程全局权限
            $.ajax({
                url: serverPath.formPath + setter.formLinkPath +"formDef/getPermissionByActDefId",
                type: 'post',
                async: false, //更改为同步
                dataType: 'json',
                data: {'formKey':formKey,'actDefId':actDefId,'parentActDefId': parentActDefId},
                success: function (result) {
                    dealResult(result);
                }
            });

        }else{
            // 自定义表单权限
            $.ajax({
                url: serverPath.formPath + setter.formLinkPath +"formDef/getPermissionByTableFormKey",
                type: 'post',
                async: false, //更改为同步
                dataType: 'json',
                data: {'formKey':formKey},
                success: function (result) {
                    dealResult(result);

                    //var tableShow = result.tableShow;
                    //var opinion = result.opinion;
                }
            });
        }

        function dealResult(result) {
            res = result;
            //加载主表
            if(result.field.length>0){
                loadTable(result.field);
            }
            //加载子表
            if(result.table.length>0){
                $("#mainTable").append(
                    '<br><div style="overflow:auto;width: 100%;" id="tableHead">\n' +
                    '    <div style="padding-left: 33%">\n' +
                    '        <input type="radio" name="subRadio" lay-filter="subRadio" value="1" title="隐藏">\n' +
                    '        <input type="radio" name="subRadio" lay-filter="subRadio" value="2" title="只读">\n' +
                    '        <input type="radio" name="subRadio" lay-filter="subRadio" value="3" title="编辑">\n' +
                    '        <input type="radio" name="subRadio" lay-filter="subRadio" value="4" title="必填">\n' +
                    '    </div>\n' +
                    '</div>'
                );
                for(var i = 0; i < result.table.length; i++){
                    for(var j = 0; j < result.tableShow.length; j++){
                        if(result.table[i].tableId==result.tableShow[j].tableId){
                            var y;//子表隐藏
                            var b;//子表必填
                            var addRow;//允许添加行
                            var subTable = "subTable"+i;//设置tableId
                            /*var subCheckBox0 = result.tableShow[j].tableId+0;
                            var subCheckBox1 = result.tableShow[j].tableId+1;
                            var subCheckBox2 = result.tableShow[j].tableId+2;
                            if(result.tableShow[j].show.y=='true'){
                                y = '    <input type="checkbox" lay-skin="primary" lay-filter="subCheckBox" name="'+subCheckBox0+'" checked title="子表隐藏">\n';
                            }else{
                                y = '    <input type="checkbox" lay-skin="primary" lay-filter="subCheckBox" name="'+subCheckBox0+'" title="子表隐藏">\n';
                            }
                            if(result.tableShow[j].show.b=='true'){
                                b = '    <input type="checkbox" lay-skin="primary" lay-filter="subCheckBox" name="'+subCheckBox1+'" checked title="子表必填">\n';
                            }else{
                                b = '    <input type="checkbox" lay-skin="primary" lay-filter="subCheckBox" name="'+subCheckBox1+'" title="子表必填">\n';
                            }
                            if(result.tableShow[j].show.addRow=='true'){
                                addRow = '    <input type="checkbox" lay-skin="primary" lay-filter="subCheckBox" name="'+subCheckBox2+'" checked title="允许添加行">\n';
                            }else{
                                addRow = '    <input type="checkbox" lay-skin="primary" lay-filter="subCheckBox" name="'+subCheckBox2+'" title="允许添加行">\n';
                            }*/

                            $("#tableHead").append(
                                '<div style="text-align:center">\n' +
                                '    <span>'+result.table[i].memo+'</span>' +
                                //y+b+addRow+
                                '</div>\n' +
                                '<table class="layui-hide" id='+subTable+' lay-filter='+subTable+'></table>\n'
                            );
                            loadSubTable(result.table[i].subField,"#"+subTable);
                        }
                    }
                }
                form.render();
            }
        }

        //加载主表
        function loadTable(res){
            table.render({
                elem: '#table',
                data: res,
                page:false,
                limit:10000,
                cols: [[
                    {field: 'memo', title: '字段'},
                    {field: 'read', title: '只读权限',templet: '#selectReadTemplate'},
                    {field: 'edit', title: '编辑权限',templet: '#selectWriteTemplate'},
                    {field: 'write', title: '必填权限',templet: '#selectRequiredTemplate'},
                    {field: 'readCommit', title: '只读提交',templet: '#radioTemplate'}
                ]],
                done: function (res, curr, count) {
                    // 循环表格中每一行
                    $.each(res.data,function(i,item){
                        //select回显
                        layui.each($("select[name='"+res.data[i].tableId+res.data[i].title+"']"), function (index, item) {
                            var elem = $(item);
                            if(index==0){
                                elem.val(res.data[i].read.type);
                                elem.attr('id',res.data[i].tableId+res.data[i].title+0);
                            }
                            if(index==1){
                                elem.val(res.data[i].write.type);
                                elem.attr('id',res.data[i].tableId+res.data[i].title+1);
                            }
                            if(index==2){
                                elem.val(res.data[i].required.type);
                                elem.attr('id',res.data[i].tableId+res.data[i].title+2);
                            }
                        });
                        //checkbox回显
                        layui.each($("input[name='"+res.data[i].tableId+res.data[i].title+"']"), function (index, item) {
                            var elem = $(item);
                            elem.prop("checked",res.data[i].rpost);
                            elem.attr('id',res.data[i].tableId+res.data[i].title+3);
                        });
                    });
                    form.render('select');
                }
            });
        }
        //加载子表
        function loadSubTable(res,tableId){
            table.render({
                elem: tableId,
                data: res,
                page:false,
                limit:10000,
                cols: [[
                    {field: 'memo', title: '子表字段'},
                    {field: 'read', title: '只读权限',templet: '#selectReadTemplate'},
                    {field: 'edit', title: '编辑权限',templet: '#selectWriteTemplate'},
                    {field: 'write', title: '必填权限',templet: '#selectRequiredTemplate'}
                ]],
                done: function (res, curr, count) {
                    // 循环表格中每一行
                    $.each(res.data,function(i,item){
                        //select回显
                        layui.each($("select[name='"+res.data[i].tableId+res.data[i].title+"']"), function (index, item) {
                            var elem = $(item);
                            if(index==0){
                                elem.val(res.data[i].read.type);
                                elem.attr('id',res.data[i].tableId+res.data[i].title+0);
                            }
                            if(index==1){
                                elem.val(res.data[i].write.type);
                                elem.attr('id',res.data[i].tableId+res.data[i].title+1);
                            }
                            if(index==2){
                                elem.val(res.data[i].required.type);
                                elem.attr('id',res.data[i].tableId+res.data[i].title+2);
                            }
                        });
                    });
                    form.render('select');
                }
            });
        }
    });
</script>
<style>
    #permission .layui-table-cell{
        height: auto;
    }
    .layui-table-tool-temp{
        padding-right:0;
    }
</style>
</body>
</html>